package com.hdax.dm.ribbitMQ.listener;

import com.hdax.dm.feign.LinkUserFeign;
import com.hdax.dm.feign.SchedulerFeign;
import com.hdax.dm.order.DmOrder;
import com.hdax.dm.scheduler.DmSchedulerSeat;
import com.hdax.dm.service.OrderLinkUserService;
import com.hdax.dm.service.OrderService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@RabbitListener(queues = {"dm_order_dlx_queue","dm_seat_dlx_queue","dm_linkUser_dlx_queue"})
@Component
public class MyRabbitListener {

    @Autowired
    private SchedulerFeign schedulerFeign;

    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderLinkUserService linkUserService;
    @RabbitHandler
    public void rollbackLinkUser(Channel channel, Message message, String orderNo) throws IOException {
        redisTemplate.delete("lock");
        String queue = message.getMessageProperties().getConsumerQueue();
        long tag = message.getMessageProperties().getDeliveryTag();
        if(queue.equals("dm_seat_dlx_queue")) {
            Boolean sche =schedulerFeign.updataXY(orderNo);
            if (sche){
                //消费
                channel.basicAck(tag,false);
                System.out.println("订单：" + orderNo + "，座位状态已经修改");
            }else{//没修改成功
                //basicNack(tag, 是否批量, 是否重回队列)
                channel.basicNack(tag,false,false);
                System.out.println("订单：" + orderNo + "，座位状态修改失败");
            }

        } else if (queue.equals("dm_linkUser_dlx_queue")) {
            channel.basicAck(tag,false);
            //座位状态修改
            Boolean sche = schedulerFeign.updataXY(orderNo);
            //订单联系人删除
            Boolean link =  linkUserService.delectLinkUser(orderNo);
            System.out.println("订单：" + orderNo + "，订单联系人已经删除");


        }else if (queue.equals("dm_order_dlx_queue")){
            Boolean sche = schedulerFeign.updataXY(orderNo);
            System.out.println("订单：" + orderNo + "，座位状态已经修改");
            Boolean link =  linkUserService.delectLinkUser(orderNo);
            System.out.println("订单：" + orderNo + "，订单联系人已经删除");

            Boolean b = orderService.orderType(orderNo);
            System.out.println("订单：" + orderNo + "，订单超时已经修改");
        } else if (queue.equals("dm_orderDel_dlx_queue")) {
            Boolean sche = schedulerFeign.updataXY(orderNo);
            System.out.println("订单：" + orderNo + "，座位状态已经修改");
            Boolean link =  linkUserService.delectLinkUser(orderNo);
            System.out.println("订单：" + orderNo + "，订单联系人已经删除");

            Boolean order =  orderService.deleteOrder(orderNo);
            System.out.println("订单：" + orderNo + "，订单已经删除");
        }
    }

}
